package com.taptech.doufu.ui.view;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.widget.TextView;

import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.util.TextAtClickHelper;
import com.taptech.doufu.util.TextLinkClickHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longsiyang on 15/11/16.
 */
public class DiaobaoTextView extends TextView
{


    Context mContext;

    // 表示文字有哪些可点击模式
    byte mClickMode = 0;
    static final public byte AtClickEnable = 0x01;
    static final public byte LinkClickEnable = 0x02;

    // special range
    List<int[]> specialIndexList;


    public DiaobaoTextView(Context context)
    {

        super(context);
        mContext = context;
        initView();
    }

    public DiaobaoTextView(Context context, AttributeSet attrs)
    {

        super(context, attrs);
        mContext = context;
        initView();
    }

    public DiaobaoTextView(Context context, AttributeSet attrs, int defStyleAttr)
    {

        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }


    private void initView()
    {
        specialIndexList = new ArrayList<int[]>();


    }

    /**
     * 设置支持的点击模式
     *
     * @param clickmode
     */
    public void setClickMode(byte clickmode)
    {

        mClickMode = clickmode;
        //updateTextViewUi(clickmode);
    }



    private CharSequence getSpanCharSequence(byte clickMode, CharSequence content)
    {

        CharSequence charSequence = content;
        if ((clickMode & LinkClickEnable) != 0)
        {
            charSequence = TextLinkClickHelper.getSpanString(mContext, this, content, specialIndexList);
        }

        // 允许at点击
        if ((clickMode & AtClickEnable) != 0)
        {
            TextAtClickHelper.getSpanString(this, charSequence);


        }
        return charSequence;
    }

    public void setDiaobaoText(CharSequence text)
    {
        if (text != null && !TextUtil.isEmpty(text.toString()))
        {
            CharSequence charSequence = getSpanCharSequence(mClickMode, text);
            this.setText(charSequence);
        }
    }

    public void setDiaobaoText(CharSequence text, int length)
    {
        try
        {
            if (text != null && !TextUtil.isEmpty(text.toString()))
            {
                CharSequence charSequence = getSpanCharSequence(mClickMode, text);
                if (charSequence.length() > length)
                {
                    int displayLength = isInSpecialList(length);
                    SpannableStringBuilder ssb = new SpannableStringBuilder(charSequence);
                    ssb.delete(displayLength, ssb.length());
                    ssb.append("...");
                    this.setText(ssb);
                } else
                {
                    SpannableStringBuilder ssb = new SpannableStringBuilder(charSequence);
                    this.setText(ssb);
                }

            }
        } catch (Exception e)
        {
            e.printStackTrace();

        }

    }




    /**
     *
     * @param index 显示文字中的位置
     * @return 如果不在特殊段落中，返回index，如果在特殊段落中，返回特殊段落的end
     */
    private int isInSpecialList(int index){
        for (int[] specialIndex : specialIndexList){
            if (specialIndex[0] <= index && index <= specialIndex[1]){
                return  specialIndex[1];
            }
        }
        return index;
    }





}

